package edu.towson.cosc.cosc455.kchmura.compiler;

import edu.towson.cosc.cosc455.interfaces.LexicalAnalyzer;

/**
 * @author kchmur1 
 * This class contains the methods for processing the characters
 * of the input file.
 *
 */
public class MyLexicalAnalyzer implements LexicalAnalyzer {

	private char nextCharacter;
	private char charSpace = ' ';
	private int currentPos = 0;
	private char[] charList = new char[500];

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.towson.cosc.cosc455.interfaces.LexicalAnalyzer#getNextToken()
	 */
	@Override
	public void getNextToken() {
		if (currentPos != compiler.lineFromFile.length()) {
			addCharacter();
			getCharacter();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.towson.cosc.cosc455.interfaces.LexicalAnalyzer#getCharacter()
	 */
	@Override
	public void getCharacter() {
		if (currentPos != compiler.lineFromFile.length()) {
			getNextToken();
		} else {
			System.err.print("Lexical Error Found.");
			System.exit(0);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.towson.cosc.cosc455.interfaces.LexicalAnalyzer#addCharacter()
	 */
	@Override
	public void addCharacter() {
		if (!isSpace(nextCharacter)) {
			charList[currentPos] = nextCharacter;
			currentPos++;
		} 
		else if(isSpace(nextCharacter) == true){
			charList[currentPos] = charSpace;
			currentPos++;
		}

		else {
			System.err.print("Lexical Error Found. Character not valid.");
			System.exit(0);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * edu.towson.cosc.cosc455.interfaces.LexicalAnalyzer#isSpace(java.lang.
	 * String)
	 */
	@Override
	public boolean isSpace(char c) {
		if (c == ' ') {
			return true;
		}
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.towson.cosc.cosc455.interfaces.LexicalAnalyzer#lookupToken()
	 */
	@Override
	public boolean lookupToken() {
		boolean lookupBoolean = false;
		for (int k = 0; k < LexicalTokens.tokenLookupArray.size(); k++) {
			if (!compiler.token.equals(LexicalTokens.tokenLookupArray.get(k))) {
				lookupBoolean = false;
			} else {
				lookupBoolean = true;
			}

		}
		return lookupBoolean;
	}

}
